package com.catmiw.mp3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

public class Mp3File {
	private String filename;
	private RandomAccessFile raf;
	private List<Frame> frames;
	private byte[] tag;
	
	public Mp3File() {

	}

	public Mp3File(String filename) {
		this.filename = filename;
		readFrames();
	}

	public boolean readFrames() {
		if (init() == false)
			return false;

		frames = new ArrayList<Frame>();
		int framecount=0;

		try {
			long len = raf.length();

			
			while (raf.getFilePointer() < len) {
				byte[] data = new byte[4];
				raf.read(data);
				Header header = new Header(data);
				if (header.parseHeader() == false) {
					long nowpos=raf.getFilePointer();
					if (nowpos-4==len-128){
						tag=new byte[128];
						raf.seek(nowpos-4);
						raf.read(tag);
					}
					break;
				} else {
					framecount++;
					int framelen = header.getFramesize();
					byte[] mdata = new byte[framelen - data.length];

					raf.read(mdata);

					Frame frame = new Frame(header, mdata);
					frames.add(frame);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//System.out.println(filename+":"+framecount);

		return true;
	}

	public void close() {
		if (raf != null) {
			try {
				raf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean init() {
		File file = new File(filename);
		if (file == null || !filename.toLowerCase().endsWith(".mp3")
				|| !(file.isFile()) || !(file.exists())) {
			return false;
		}
		if (raf == null) {
			try {
				raf = new RandomAccessFile(filename, "rw");
				raf.seek(0);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		} else {
			try {
				raf.seek(0);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return true;
	}

	public static void joinMp3Files(List<Mp3File> mp3s,String newfilename){
		RandomAccessFile ra=null;
		try {
			File file=new File(newfilename);
			file.delete();
			ra=new RandomAccessFile(file,"rw");
			
			for (Mp3File mf:mp3s){
				for (Frame f:mf.frames){
					ra.write(f.toByteArray());
				}
			}
			byte tag[]=new byte[128];
			ra.write(tag);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if (ra!=null)
				try {
					ra.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	public static String UKMP3DIR="D:/studyassist/英语mp3/英式/";
	
	public static void createEnglishWordTestMp3(List<String> words,String newfilename){
		Mp3File di=new Mp3File(UKMP3DIR+"滴.mp3");
		Mp3File silence=new Mp3File(UKMP3DIR+"静音0.1秒.mp3");
		
		List<Mp3File> mp3s =new ArrayList<Mp3File>();
		mp3s.add(di);
		for (int i=0;i<5;i++){
			mp3s.add(silence);
		}
		mp3s.add(di);
		for (int i=0;i<5;i++){
			mp3s.add(silence);
		}
		for (String word:words){
			mp3s.add(di);
			for (int i=0;i<10;i++){
				mp3s.add(silence);
			}
			String fn=UKMP3DIR+word+".mp3";
			File file=new File(fn);
			if (file==null||!file.isFile()||!file.exists()){
					System.out.println("无法找到"+word+"的MP3文件");
					continue;
			}
			Mp3File wm=new Mp3File(fn);
			for (int i=0;i<3;i++){
				mp3s.add(wm);
				for (int j=0;j<20;j++){
					mp3s.add(silence);
				}
			}
			int interval=word.length()*10;
			if (interval<40)
				interval=40;
			for (int i=0;i<interval;i++){
				mp3s.add(silence);
			}
		}
		
		for (int i=0;i<3;i++){
			mp3s.add(di);
			for (int j=0;j<5;j++){
				mp3s.add(silence);
			}
		}
		joinMp3Files(mp3s,newfilename);
	}

}
